from wsgiref import headers
from configs.configs import  *
import logging
logger = logging.getLogger(__name__)
import requests
from conftest import *

class RequestsTool:
    def __init__(self, ip,):
        self.ip = ip



    def send_request(self, casedata,token):
        #headrstoken判断
        if "token" in casedata["request"]["headers"]:
            casedata["request"]["headers"]["token"] = token
            logger.info("Request headers: {}".format(casedata["request"]["headers"]))
        elif "token" in casedata["request"]["headers"]:
            casedata["request"]["params"]["token"] = token


        headers = casedata["request"]["headers"]
        method = casedata["request"]["method"]
        url = self.ip + casedata["request"]["url"]
        params = casedata["request"]["params"]


        #请求方法判断
        try:
            if method.upper() == "GET":
                res = requests.get(url, headers=headers, params=params)
                logger.info("执行请求后，结果是：%s" % res.text)
            elif method.upper() == "POST":
                res = requests.post(url, headers=headers, params=params)
                logger.info("执行请求后，结果是：%s" % res.text)
            else:
                logger.error("不支持的请求方法: %s" % method)
                raise ValueError("不支持的请求方法: %s" % method)
        except Exception as ex:
            logger.error('出错了，错误是%s' % ex)
            raise ex
        return res
